package com.qmhl.toolposition.activity

import android.os.Build
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import com.hjq.toast.Toaster
import com.lxj.xpopup.XPopup
import com.permissionx.guolindev.PermissionX
import com.qmhl.toolposition.R
import com.qmhl.toolposition.adapter.MainAdapter
import com.qmhl.toolposition.api.UserService
import com.qmhl.toolposition.api.content.AppUpdate
import com.qmhl.toolposition.base.BaseActivity
import com.qmhl.toolposition.databinding.ActivityMainBinding
import com.qmhl.toolposition.databinding.ItemTabBinding
import com.qmhl.toolposition.event.TabSelectEvent
import com.qmhl.toolposition.utils.UpdateUtils
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus


class MainActivity : BaseActivity() {

    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        updateApp()
    }

    override fun initData() {
        super.initData()
        //禁止viewpager滑动
        binding.vpMain.isUserInputEnabled = false
        binding.apply {
            vpMain.offscreenPageLimit = indicatorTitles.size
            vpMain.adapter = MainAdapter(this@MainActivity, indicatorTitles.size)
        }

        for (i in indicatorTitles.indices) {
            ItemTabBinding.inflate(layoutInflater).apply {
                content.setText(indicatorTitles[i])
                icon.setImageResource(indicatorIcons[i])
                binding.dtMain.addView(root)
            }
        }

        ViewPager2Delegate.install(binding.vpMain, binding.dtMain, false)

        binding.vpMain.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                EventBus.getDefault().postSticky(TabSelectEvent(position))
            }
        })

    }


    companion object {

        private val indicatorTitles = intArrayOf(
            R.string.app_home, R.string.app_position, R.string.app_msg, R.string.app_mine
        )

        private val indicatorIcons = intArrayOf(
            R.drawable.selector_tab_home,
            R.drawable.selector_tab_position,
            R.drawable.selector_tab_message,
            R.drawable.selector_tab_mine
        )
    }


    private fun requestSpecPermission() {
        PermissionX.init(this)
            .permissions(
                android.Manifest.permission.ACCESS_BACKGROUND_LOCATION
            )
            .request { allGranted, grantedList, deniedList ->

            }
    }

    override fun onResume() {
        super.onResume()
        val currentVersion = Build.VERSION.SDK_INT

        if (currentVersion >= Build.VERSION_CODES.Q) {
            var granted =
                PermissionX.isGranted(this, android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)
            if (!granted) {
                requestSpecPermission()
            }
        }


    }

    private fun updateApp() {

        var versionName = UpdateUtils.getVersionName(this)
        val curVersion = versionName.toDouble()

        lifecycleScope.launch {
            var content = UserService.create()
                .updateInfo()
            if (content.code == 200) {
                val data = content.data
                isShowUpdate(curVersion, data)
            } else {
                Toaster.showShort(content.msg)
            }
        }


    }

    private fun isShowUpdate(curVersion: Double, data: AppUpdate) {
        if (data.version > curVersion) {
            XPopup.Builder(this)
                .asCustom(AppUpdatePopup(this, data.link, data.toast))
                .show()
        }
    }


}